package com.mutual.productPay.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoProductService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;
import com.mutual.productPay.pojo.ProductPay;
import com.mutual.productPay.service.ProductPayService;


@Controller
@RequestMapping("/productPay")
public class ProductPayController extends BaseController{

	@Resource
	private ProductService productService;
	
	@Resource
	private ProductPayService productPayService;
	
	@Resource
	private AppUserInfoProductService appUserInfoProductService;
	
	private static final Logger log = LoggerFactory.getLogger(ProductPayController.class);
	
	
	@RequestMapping(value = "/toSelectStatisticsProdductPayList")
	public String toSelectStatisticsProdductPayList(){
		return "productPay/selectStatisticsProdductPayList";
	}
	
	@RequestMapping(value = "/selectStatisticsProdductPayListData")
	@ResponseBody
	public ResultList<Map<String,Object>> selectStatisticsProdductPayListData(@RequestParam Map<String,Object> param){
		total=productService.selectProductTotal(param);
		ResultList<Map<String,Object>> result=new ResultList<>();
		result.setCount(total);
		List<Map<String,Object>> productPayList=productPayService.selectStatisticsProdductPayList(param);
		result.setData(productPayList);
		return result;
	}
	
	/**
	 * 扣款跳转
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/toDeduction")
	public String toEeduction(int productId,Model model){
		Product product = productService.selectByPrimaryKey(productId);
		model.addAttribute("product", product);
		return "productPay/deduction";
	}
	
	/**
	 * 扣款前序确认
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/readyDeduction")
	@ResponseBody
	public ReturnResult readyDeduction(@RequestParam Map<String,Object> param){
		ReturnResult result=new ReturnResult();
		int total = appUserInfoProductService.selectTotalCount(param);
		result.setData(total);
		return result;
	}
	
	/**
	 * 扣款
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/deduction")
	@ResponseBody
	public ReturnResult deduction(ProductPay productPay){
		ReturnResult result=null;
		try {
			productPay.setCreateDate(new Date());
			productPay.setCreator(getCurrentUser().getUserName());
			productPay.setCreatorId(getCurrentUser().getId());
			result=productPayService.deduction(productPay);
		} catch (Exception e) {
			log.error("扣款出错：", e);
			result=new ReturnResult();
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("扣款出错，请稍候重试");
		}
		return result;
	}
	
	

	/**
	 * 扣款
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/selectAppUserProductByIdCode")
	@ResponseBody
	public ReturnResult selectAppUserProductByIdCode(AppUserInfoProduct appUserInfoProduct){
		ReturnResult result=new ReturnResult();
		List<AppUserInfoProduct> appUserInfoProducts=appUserInfoProductService.selectAppUserInfoProductList(appUserInfoProduct);
		if(appUserInfoProducts.isEmpty()) {
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("此身份证号未加入该产品");
		}else {
			result.setData(appUserInfoProducts.get(0));
		}
		return result;
	}
	
	/**
	 * 产品扣款详情界面
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/toProductPayDetail")
	public String toProductPayDetail(int productId,Model model){
		Product product = productService.selectByPrimaryKey(productId);
		model.addAttribute("product", product);
		return "productPay/productPayDetail";
	}
	
	
	/**
	 * 扣款列表
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/productPayListData")
	@ResponseBody
	public ResultList<ProductPay> productPayListData(@RequestParam Map<String,Object> param){
		total=productPayService.selectProductPayCount(param);
		ResultList<ProductPay> result=new ResultList<>();
		result.setCount(total);
		List<ProductPay> productPayList=productPayService.selectProductPayList(param);
		result.setData(productPayList);
		return result;
	}
	
	
	/**
	 * 用户申请产品列表
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/appUserProductListData")
	@ResponseBody
	public ResultList<AppUserInfoProduct> appUserProductListData(@RequestParam Map<String,Object> param){
		total=appUserInfoProductService.selectTotalCount(param);
		ResultList<AppUserInfoProduct> result=new ResultList<>();
		result.setCount(total);
		List<AppUserInfoProduct> appUserInfoProduct=appUserInfoProductService.selectAppUserInfoProductList(param);
		result.setData(appUserInfoProduct);
		return result;
	}
	
	
}
